package com.bristua.barcode.customer.biz;

import com.bristua.barcode.common.exception.SystemException;
import com.bristua.barcode.common.util.RedisLock;
import com.bristua.barcode.customer.dao.TCustomerAccountMapper;
import com.bristua.barcode.customer.model.TCustomerAccount;
import com.bristua.barcode.customer.model.condition.TCustomerAccountExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/8/25.
 */
@Transactional
@Service
public class UpdateCustomerAccountBiz {

    private static final Logger logger = LoggerFactory.getLogger(UpdateCustomerAccountBiz.class);

    private final static String lock_customer_account_key = "abcdef@lock_customer_account_key";

    private final static long TIMEOUT = 10 * 1000;

    @Resource
    private TCustomerAccountMapper tCustomerAccountMapper;

    @Autowired
    private RedisLock redisLock;

    @Async
    public void updateCustomerAccount(String customerId,Long updateAmt,int type){
        logger.info("==>客户ID:"+customerId+",修改金额:"+updateAmt+",类型:"+(type==0?"下单加钱":"提现扣钱"));
        long time = System.currentTimeMillis() + TIMEOUT;
        if (redisLock.lock(lock_customer_account_key, String.valueOf(time))) {
            try {
                TCustomerAccountExample tCustomerAccountExample = new TCustomerAccountExample();
                tCustomerAccountExample.createCriteria().andCustomerIdEqualTo(customerId);
                List<TCustomerAccount> tCustomerAccounts = tCustomerAccountMapper.selectByExample(tCustomerAccountExample);
                if(tCustomerAccounts!=null && tCustomerAccounts.size()>0){
                    TCustomerAccount tCustomerAccount = tCustomerAccounts.get(0);
                    TCustomerAccount updateTCustomerAccount = new TCustomerAccount();
                    if(type==0){
                        updateTCustomerAccount.setAccountAmt(tCustomerAccount.getAccountAmt().longValue()+updateAmt.longValue());
                    }
                    if(type==1){
                        updateTCustomerAccount.setAccountAmt(tCustomerAccount.getAccountAmt().longValue()-updateAmt.longValue());
                    }

                    TCustomerAccountExample updateTCustomerAccountExample = new TCustomerAccountExample();
                    updateTCustomerAccountExample.createCriteria().andCustomerAccountIdEqualTo(tCustomerAccount.getCustomerAccountId()).andAccountAmtEqualTo(tCustomerAccount.getAccountAmt());
                    tCustomerAccountMapper.updateByExample(updateTCustomerAccount,updateTCustomerAccountExample);
                }
            }catch (Exception ex){
                redisLock.unluck(lock_customer_account_key, String.valueOf(time));
                throw new SystemException(ex);
            }
            redisLock.unluck(lock_customer_account_key, String.valueOf(time));
        }else{
            updateCustomerAccount(customerId,updateAmt,type);
        }
    }
}
